<?php
namespace App\Admin\Controllers\Basics;

use App\Models\Basics\ExpressChannel;
use App\Models\Basics\SysLArea;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
class ExpressChannelController extends AdminController{

    protected $title = '物流渠道';

    protected function grid()
    {
        $grid = new Grid(new ExpressChannel());
        $grid->model()->orderBy('id','DESC');
        $grid->column('id','ID')->style('width: 5%');
        $grid->column('name','名称');
        $grid->column('area.area','站点');
        $grid->column('transport','运输方式')->display(function ($value){
            return ExpressChannel::$_transport[$value];
        })->filter(ExpressChannel::$_transport);
        $grid->column('days','时效（天）')->help('默认到货时效');
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableDelete();
            $actions->disableView();
        });

        $grid->disableRowSelector();
        $grid->disableExport();
        $grid->disableCreateButton();
        $grid->quickCreate(function (Grid\Tools\QuickCreate $create) {
            $create->text('name', '名称');
            $create->select('area_id', '站点')->options(SysLArea::options());
            $create->select('transport', '运输方式')->options(ExpressChannel::$_transport);
            $create->text('days','时效（天）');
        });
        $grid->filter(function($filter){
            $filter->column(1/3, function ($filter) {
                $filter->where(function ($query) {
                    $query->where('name', 'like', "%{$this->input}%");
                }, '名称')->placeholder('模糊查询名称');
            });
            $filter->column(1/3, function ($filter) {
                $filter->equal('area_id','站点')->select(SysLArea::options());
            });
        });
        return $grid;
    }

    public function form()
    {
        $form = new Form(new ExpressChannel());

        $form->hidden('id');

        $form->text('name', '名称')
            ->setWidth(3)
            ->creationRules(['required', "unique:b_express_channel"],['required' => '名称必填','unique' => '名称已存在'])
            ->updateRules(['required', "unique:b_express_channel,name,{{id}}"],['required' => '名称必填','unique' => '名称已存在']);
        $form->select('area_id','站点')->options(SysLArea::options())->required()->rules('required',['required' => '站点必填'])->setWidth(3);
        $form->select('transport','运输方式')->options(ExpressChannel::$_transport)->rules('required',['required' => '运输方式必填'])->required()->setWidth(3);
        $form->text('days','时效（天）')->setWidth(3);
        $form->tools(function (Form\Tools $tools) {
            $tools->disableView();
            $tools->disableDelete();
        });
        return $form;
    }
}
